<?php 
class class_danhmuc extends database
{
	var $sqlHienThiSanPham = '';
	
	function HienThiDanhMucCap1()
	{
		$this->setQuery('set names utf8');
		$this->Query();

		$sql = 'select * from danh_muc_san_pham where MaDanhMucCha is NULL';

		$this->setQuery($sql);
		$table = $this->LoadAllRow();
		return $table;		
	}
	
	function HienThiDanhMucCap2TroLen($MaDanhMucCha)
	{
		$this->setQuery('set names utf8');
		$this->Query();

		$sql = 'select * from danh_muc_san_pham where MaDanhMucCha='.$MaDanhMucCha;

		$this->setQuery($sql);
		$table = $this->LoadAllRow();
		return $table;		
	}
	
	function GetNumRowsDanhMucCap2TroLen($MaDanhMucCha)
	{
		$sql = 'select * from danh_muc_san_pham where MaDanhMucCha='.$MaDanhMucCha;
		$this->setQuery($sql);
		$tb = $this->LoadAllRow();
		$num = count($tb);
		return $num;
	}
	
	function CauTruyVanHienThiSanPham($MaDanhMuc)
	{
		$tb = $this->HienThiDanhMucCap2TroLen($MaDanhMuc);
		if (count($tb) == 0)
		{
			return;
		}
		foreach ($tb as $row)
		{
			$this->sqlHienThiSanPham .= ' union 
										select s.MaSo, s.TenSanPham, d.TenDanhMuc, n.Username, s.GiaSanPham, dv.TenDonViTinh, s.SoLuong, s.NhaSanXuat, s.HinhAnh, ROUND(s.DiemDanhGia,2) as DiemDanhGia, s.NgayDang, s.PageView, s.HaveTag  
									from san_pham s, danh_muc_san_pham d, nguoi_ban n, don_vi_tinh dv 
									where s.MaDanhMuc='.$row['MaDanhMuc'].' and s.MaDanhMuc = d.MaDanhMuc and s.MaNguoiBan = n.MaSo and s.MaDonViTinh=dv.MaSo 
									and s.TinhTrangDang=1 and s.TinhTrangXoa=0';
			$this->CauTruyVanHienThiSanPham($row['MaDanhMuc']);
		}
	}
	
	function HienThiSanPhamTheoMaDanhMuc($MaDanhMuc,$start,$limit)
	{
		$this->sqlHienThiSanPham = '';
		
		$this->setQuery('set names utf8');
		$this->Query();
		
		$this->sqlHienThiSanPham .= 'select s.MaSo, s.TenSanPham, d.TenDanhMuc, n.Username, s.GiaSanPham, dv.TenDonViTinh, s.SoLuong, s.NhaSanXuat, s.HinhAnh, ROUND(s.DiemDanhGia,2) as DiemDanhGia, s.NgayDang, s.PageView, s.HaveTag  
									from san_pham s, danh_muc_san_pham d, nguoi_ban n, don_vi_tinh dv 
									where s.MaDanhMuc='.$MaDanhMuc.' and s.MaDanhMuc = d.MaDanhMuc and s.MaNguoiBan = n.MaSo and s.MaDonViTinh=dv.MaSo 
									and s.TinhTrangDang=1 and s.TinhTrangXoa=0';
		$this->CauTruyVanHienThiSanPham($MaDanhMuc);
		$this->sqlHienThiSanPham .= ' order by NgayDang desc limit '.$start.','.$limit ;

		$this->setQuery($this->sqlHienThiSanPham);
		$table = $this->LoadAllRow();
		return $table;
	}
	
	function CountHienThiSanPhamTheoMaDanhMuc($MaDanhMuc)
	{
		$this->sqlHienThiSanPham = '';

		$this->sqlHienThiSanPham .= 'select s.MaSo, s.TenSanPham, d.TenDanhMuc, n.Username, s.GiaSanPham, dv.TenDonViTinh, s.SoLuong, s.NhaSanXuat, s.HinhAnh, ROUND(s.DiemDanhGia,2) as DiemDanhGia, s.NgayDang, s.PageView, s.HaveTag  
									from san_pham s, danh_muc_san_pham d, nguoi_ban n, don_vi_tinh dv 
									where s.MaDanhMuc='.$MaDanhMuc.' and s.MaDanhMuc = d.MaDanhMuc and s.MaNguoiBan = n.MaSo and s.MaDonViTinh=dv.MaSo 
									and s.TinhTrangDang=1 and s.TinhTrangXoa=0';
		$this->CauTruyVanHienThiSanPham($MaDanhMuc);
		$this->sqlHienThiSanPham .= ' order by NgayDang desc';

		$this->setQuery($this->sqlHienThiSanPham);
		$table = $this->LoadAllRow();
		$num = count($table);
		return $num;
	}
	
	function ListTagByMaSanPham($MaSanPham)
	{
		$this->setQuery('set names utf8');
		$this->Query();

		$sql = 'select * 
				from chi_tiet_tag c, tag t
				where c.MaSanPham='.$MaSanPham.' and c.MaTag = t.MaSo';

		$this->setQuery($sql);
		$table = $this->LoadAllRow();
		return $table;		
	}

}
?>